package com.study.zzedu.ui.forgetpwd;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.ui.home.HomeActivity;
import com.study.zzedu.ui.login.LoginActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.SharedPreferencesHelper;
import com.study.zzedu.utils.StringUtils;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.utils.Utils;
import com.study.zzedu.widget.LoadingDialog;

import org.json.JSONArray;
import org.json.JSONObject;

import static com.study.zzedu.utils.Utils.rexCheckPassword;


public class ForgetPwdActivity extends BaseMvpActivity<ForgetContract.IView, ForgetPwdPresenter> implements ForgetContract.IView, View.OnClickListener{

    private Button btnNext,btnNext0,btnModify,btn_getcode;
    private RelativeLayout btn_back;
    private EditText etPhoneCode,etPwd,etPwdAgain,et_stu_num;
    private LinearLayout llFirst,llSecond,llAdd;
    private Boolean backFlag = true;

    private EditText et_code;
    private ImageView iv_code;
    private TextView tv_phone_warn;

    private CountDownTimer countDownTimer = new CountDownTimer(60*1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            String value = String.valueOf((int) (millisUntilFinished / 1000));
            btn_getcode.setText(value+"s后重新获取");
        }

        @Override
        public void onFinish() {
            btn_getcode.setText("获取验证码");
            btn_getcode.setEnabled(true);
        }
    };
    @Override
    protected int getLayoutID() {
        return R.layout.activity_forgetpwd;
    }

    @Override
    protected ForgetPwdPresenter createPresenter() {
        return new ForgetPwdPresenter();
    }

    @Override
    protected ForgetContract.IView createView() {
        return this;
    }

    @Override
    protected void initView() {
        backFlag = true;
        btn_back = findViewById(R.id.rl_back);

        tv_phone_warn = findViewById(R.id.tv_phone_warn);
        llFirst = findViewById(R.id.ll_forget_first);
        llSecond = findViewById(R.id.ll_forget_second);
        llAdd = findViewById(R.id.ll_add);
        btn_getcode = findViewById(R.id.btn_getcode);
        btnNext = findViewById(R.id.btn_next);
        btnModify  = findViewById(R.id.btn_modify);
        btnNext0 = findViewById(R.id.btn_next0);
        etPhoneCode = findViewById(R.id.et_phone_code);
        etPwd = findViewById(R.id.et_pwd);
        etPwdAgain = findViewById(R.id.et_pwd_again);
        et_stu_num = findViewById(R.id.et_stu_num);
        et_code = findViewById(R.id.et_code);
        iv_code = findViewById(R.id.iv_code);
    }

    @Override
    protected void initData() {
        autoProgress(true);
        getPresenter().getImageCode(ForgetPwdActivity.this);
    }

    @Override
    protected void initListener() {
        btnNext.setOnClickListener(this);
        btnModify.setOnClickListener(this);
        btn_back.setOnClickListener(this);
        btn_getcode.setOnClickListener(this);
        iv_code.setOnClickListener(this);
        btnNext0.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_code:
                autoProgress(true);
                getPresenter().getImageCode(ForgetPwdActivity.this);
                break;
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_next:
                if(TextUtils.isEmpty(AppConstant.codeUUID))
                {
                    ToastUtil.showShortToast("codeUUID 为 null");
                    return;
                }
                if(!(StringUtils.isNumeric(etPhoneCode.getText().toString()))){
                    ToastUtil.showShortToast("验证码输入异常");
                    return;
                }
                autoProgress(true);
                getPresenter().identify(ForgetPwdActivity.this,etPhoneCode.getText().toString(),AppConstant.codeUUID);

                break;
            case R.id.btn_modify:
                if(TextUtils.isEmpty(AppConstant.idCodeUUID)){
                    ToastUtil.showShortToast("idCodeUUID 不可为空");
                    return;
                }
                if(TextUtils.isEmpty(etPwd.getText().toString().trim()) || TextUtils.isEmpty(etPwdAgain.getText().toString().trim())){
                    ToastUtil.showShortToast("输入不可为空");
                    return;
                }
                if(!etPwd.getText().toString().trim().equals(etPwdAgain.getText().toString().trim())){
                    ToastUtil.showShortToast("两次输入不同");
                    return;
                }
                if(!rexCheckPassword(etPwd.getText().toString().trim())){
                    ToastUtil.showShortToast("大小写字母,数字,特殊字符中的至少4种.8位以上");
                    return;
                }
                autoProgress(true);
                getPresenter().modify(ForgetPwdActivity.this,Utils.digest(etPwd.getText().toString().trim()));
                break;
            case R.id.btn_getcode:
                getPresenter().getPhoneCode(ForgetPwdActivity.this);
                btn_getcode.setEnabled(false);
                //调用 CountDownTimer 对象的 start() 方法开始倒计时，也不涉及到线程处理
                countDownTimer.start();
                break;
            case R.id.btn_next0:
                if(TextUtils.isEmpty(et_code.getText().toString())){
                    ToastUtil.showShortToast("验证码不能为空");
                    return;
                }
                if(TextUtils.isEmpty(et_stu_num.getText().toString())){
                    ToastUtil.showShortToast("学号不能为空");
                    return;
                }
                if(TextUtils.isEmpty(codeUUID)){
                    ToastUtil.showShortToast("codeUUID为空");
                    return;
                }
                getPresenter().getPhoneByUserCode(ForgetPwdActivity.this,et_stu_num.getText().toString(),et_code.getText().toString(),codeUUID);
                break;
        }
    }

    @Override
    public void getData(String data) {
        ToastUtil.showShortToast("验证码已发送");
    }

    @Override
    public void showError(String error) {
        autoProgress(false);
        getPresenter().getImageCode(ForgetPwdActivity.this);
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    @Override
    public void getIdentifyData(String name) {
        autoProgress(false);
        backFlag = false;
        llFirst.setVisibility(View.INVISIBLE);
        llSecond.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(countDownTimer != null)
            countDownTimer.cancel();
    }

    @Override
    public void getModifyData(String name) {
        autoProgress(false);
        ToastUtil.showShortToast("修改密码成功");
        finish();
    }
    String codeUUID = "";
    @Override
    public void onCodeReturn(String data) {
        autoProgress(false);
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            if(jArray == null)
                return;
            if(jArray.length() == 0)
                return;
            String d = jArray.getJSONObject(0).getString("img");

            iv_code.setBackground(new BitmapDrawable(Utils.stringToBitmap(d)));
            codeUUID = jArray.getJSONObject(0).getString("uuid");
        }catch (Exception e){
            e.toString();
        }
    }

    @Override
    public void onGetPhoneByUserCode(String data) {
        llAdd.setVisibility(View.INVISIBLE);
        llFirst.setVisibility(View.VISIBLE);
        llAdd.setVisibility(View.INVISIBLE);
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            if(jArray == null)
                return;
            if(jArray.length() == 0)
                return;

            AppConstant.codeKey = jArray.getJSONObject(0).getString("uuid");
            AppConstant.phone = jArray.getJSONObject(0).getString("phone");
            tv_phone_warn.setText("您的"+AppConstant.phone+"手机号");

            //自动发送验证码
            getPresenter().getPhoneCode(ForgetPwdActivity.this);
            btn_getcode.setEnabled(false);
            //调用 CountDownTimer 对象的 start() 方法开始倒计时，也不涉及到线程处理
            countDownTimer.start();
        }catch (Exception e){
            e.toString();
        }
    }

    @Override
    public void onImageCodeError(String error) {
        autoProgress(false);
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    private LoadingDialog mDialog;
    private void autoProgress(boolean show){
        if(mDialog==null){
            mDialog = new LoadingDialog(ForgetPwdActivity.this);
            //mDialog.setMessage("正在加载数据。。。");
        }
        if(show){
            mDialog.show();
        }else{
            mDialog.dismiss();
        }
    }
}
